const app = getApp()

import {
        getInfo,
} from '../../api/member'

Component({
        data: {
                groupBuy:'0',
                balance:'0.00',//库存余额
                cardBalance: '0.00', //账户余额
                benefits: '0.00', //福利金
                point: '0.00',
                couponCode: 0,
                matchPoint: 0,
                waterPrice:0,
                VERSION: app.VERSION,
                groupBuyDisplay:'',
                qrcodeLogin:app.QRCODELOGIN
        },
        pageLifetimes: {
                show: function () {
                        this.getInfo()
                        this.getGroupBuyDisplay()
                }
        },
        methods: {
                getGroupBuyDisplay(){
                       this.data.groupBuyDisplay = app.globalData.groupBuyDisplay
                       this.setData({
                        groupBuyDisplay:this.data.groupBuyDisplay
                       })
                },
                getInfo() {
                        getInfo().then( res => {
                                var balance = res.data.balance;
                                var cardBalance = res.data.cardBalance;
                                var benefits = res.data.benefits;
                                var groupBuy = res.data.groupBuy;
                                balance = parseFloat(balance).toFixed(2)
                                cardBalance = parseFloat(cardBalance).toFixed(2)
                                benefits = parseFloat(benefits).toFixed(2)
                                groupBuy = parseInt(groupBuy)
                                this.data.point = parseFloat(res.data.point/100).toFixed(2)
                                this.setData({
                                        point: this.data.point,
                                        couponCode: res.data.couponCode,
                                        matchPoint: res.data.matchPoint,
                                        balance:balance,
                                        cardBalance:cardBalance,
                                        benefits:benefits,
                                        groupBuy:groupBuy
                                })
                          })
                },
                toSaveCard: function (e) {
                    if (this.data.qrcodeLogin==true) {
                      return;
                    }
                        this.checkLogin(() => {
                                wx.navigateTo({
                                        url: '/pages/wallet/jumpPage/saveCard/index',
                                        success: function (res) { },
                                        fail: function (res) { },
                                        complete: function (res) { },
                                })
                        })
                },

                toPione: function (e) {
                  if (this.data.qrcodeLogin==true) {
                    return;
                  }
                        this.checkLogin(() => {
                                wx.navigateTo({
                                        url: '/pages/wallet/jumpPage/distribution/index',
                                })
                        })
                },
                // 跳转到库存
                toWarehouse: function (e) {
                  if (this.data.qrcodeLogin==true) {
                    return;
                  }
                        this.checkLogin(() => {
                                wx.navigateTo({
                                        url: '/pages/member/warehouse/warehouse?balance='+this.data.warehouse,
                                })
                        })
                },
                // 跳转到兑换券
                toExchangeCertificate: function (e) {
                  if (this.data.qrcodeLogin==true) {
                    return;
                  }
                        this.checkLogin(() => {
                                wx.navigateTo({
                                        url: '/pages/wallet/jumpPage/exchangeCertificate/exchangeCertificate',
                                })
                        })
                },
                //跳转到福利金的页面
                toShopCard: function (e) {
                  if (this.data.qrcodeLogin==true) {
                    return;
                  }
                        if (app.VERSION === 0 || app.VERSION === 2) {
                                this.checkLogin(() => {
                                        wx.navigateTo({
                                                url: '/pages/member/shopCard/shopCard',
                                                success: function (res) { },
                                                fail: function (res) { },
                                                complete: function (res) { },
                                        })
                                })
                        } else if (app.VERSION === 1) {
                                this.checkLogin(() => {
                                        wx.navigateTo({
                                                url: '/pages/member/emptyBarrel/emptyBarrel',
                                                success: function (res) { },
                                                fail: function (res) { },
                                                complete: function (res) { },
                                        })
                                })
                        }
                },
                toVouchers() {
                  if (this.data.qrcodeLogin==true) {
                    return;
                  }
                        this.checkLogin(() => {
                                wx.navigateTo({
                                        url: '/pages/member/couponCodeGift/index?currentTab=0',
                                })
                        })
                },
                toIntegral() {
                  if (this.data.qrcodeLogin==true) {
                    return;
                  }
                        this.checkLogin(() => {
                                wx.navigateTo({
                                        url: '/pages/wallet/jumpPage/integral/integral',
                                })
                        })
                },
                checkLogin(callback) {
                        console.info("全局变量app.globalData--" + JSON.stringify(app.globalData))
                        if (app.globalData.token == null) {
                                wx.navigateTo({
                                        url: '/pages/login/index',
                                })
                        } else {
                                if (callback) {
                                        callback()
                                }
                        }
                },
        }
})
